/*
 * con_state.h
 *
 *  Created on: Sep 17, 2010
 *      Author: khanton
 */

#ifndef CON_STATE_H_
#define CON_STATE_H_

#include <boost/date_time/posix_time/posix_time.hpp>
#include <sys/types.h>

struct con_state
{
		enum job_mode_t
		{
			jm_RP,	// 0
			jm_KU,  // 1
			jm_DR,  // 2
			jm_VPU, // 3
			jm_ZU,	// 4
			jm_RU,	// 5
			jm_PRG,	// 6

			jm_NOT_CONNECT	= 10,
			jm_PWR_FAILURE	= 11

		};

		// 0 – РП , 1-КУ,   2-ДР,     3-ВПУ,   4 – ЗУ,    5-РУ , 6 –Принудительный Вызов  Программ

		typedef u_int16_t controller_id;

		typedef struct
		{
				u_int8_t sign_;
				u_int8_t rtc_hour_;
				u_int8_t rtc_min_;
				u_int8_t rtc_sec_;
				u_int8_t rtc_day_;
				u_int8_t rtc_month_;
				u_int8_t rtc_year_;
				u_int8_t rtc_day_of_week_;

				u_int8_t flash_status_;
				u_int8_t job_mode_;

				u_int8_t current_program_;
				u_int8_t current_phase_;

				u_int8_t fault_type_;
				u_int8_t fault_red_;
				u_int8_t fault_yellow_;
				u_int8_t fault_green_;

				u_int8_t no_program_;
				u_int8_t phase_time_;

				u_int8_t power_alarm_;

				u_int8_t reserv_[8];

		}__attribute__ ((packed)) status_packet_t;

		time_t last_access_time_;

		u_int8_t rtc_hour_;
		u_int8_t rtc_min_;
		u_int8_t rtc_sec_;
		u_int8_t rtc_day_;
		u_int8_t rtc_month_;
		u_int8_t rtc_year_;
		u_int8_t rtc_day_of_week_;

		u_int8_t flash_status_;
		job_mode_t job_mode_;

		u_int8_t current_program_;
		u_int8_t current_phase_;

		u_int8_t fault_type_;
		u_int8_t fault_red_;
		u_int8_t fault_yellow_;
		u_int8_t fault_green_;

		u_int8_t no_program_;
		u_int8_t phase_time_;

		u_int8_t power_alarm_;

		explicit con_state();
		con_state(const con_state& _state);
		con_state(const con_state::status_packet_t &_state);

		~con_state();

		con_state &operator =(const con_state &_state);
		con_state &operator =(const con_state::status_packet_t &_state);

		void setup_packet(con_state::status_packet_t &_state) const;
		boost::posix_time::ptime as_local_time() const;

		static const char *job_mode_desc(const job_mode_t _mode);

	private:
		void assign(const con_state &_state);
		void assign(const con_state::status_packet_t &_state);
};

#endif /* CON_STATE_H_ */
